package dao;

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import entity.User;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import util.MongoDBUtil;

import java.util.ArrayList;
import java.util.List;

public class UserDAO {
    //添加一个用户到mongo
    //添加： 将参数u对象的数据，添加到mongodb中
    public void insert(User user) {
        //连接mongo的database 获取collection
        MongoCollection<Document> users = MongoDBUtil.getCollection("users");
        //将对象转换为document
        Document doc = new Document();
        doc.put("username", user.getUsername());
        doc.put("password", user.getPassword());
        doc.put("age", user.getAge());
        //执行insertOne
        users.insertOne(doc);
    }
    //根据ID查找 找出User对象
    public User findByID(String id) {
        //获取User
        MongoCollection<Document> users = MongoDBUtil.getCollection("users");
        //创建query查询
        Bson query = Filters.eq("_id", new ObjectId(id));
        //执行查询 Document
        FindIterable<Document> docs = users.find(query);
        User user = new User();
        for (Document doc : docs) {
            String _id =(String) doc.get("_id");
            String username=(String) doc.get("username");
            String password=(String) doc.get("password");
            Integer age=(Integer) doc.get("age");
            String email=(String) doc.get("email");
            String mobile=(String) doc.get("mobile");
            String address=(String) doc.get("address");
            int status=(Integer) doc.get("status");
            user.setId(_id);
            user.setUsername(username);
            user.setPassword(password);
            user.setAge(age);
            user.setEmail(email);
            user.setMobile(mobile);
            user.setAddress(address);
            user.setStatus(status);
        }
        return user;
    }
    //根据id删除，并告知删除数据的条数
    //返回值类型 long（根据id从数据库中删除了多少条）
    //参数 String id
    public long deleteByID(String id) {
        //获得操作mongo的collection users
        MongoCollection<Document> users = MongoDBUtil.getCollection("users");
        //调用users的删除
        Bson query = Filters.eq("_id", new ObjectId(id));
        DeleteResult result = users.deleteOne(query);
        //本次删除了多少条
        long count = result.getDeletedCount();
        return count;
    }
    //根据id修改
    //返回值 void long
    //参数String id
    public long updateByID(String id, int status) {
        //获得操作mongo的collection users
        MongoCollection<Document> users = MongoDBUtil.getCollection("users");
        //修改条件
        Bson query = Filters.eq("_id", new ObjectId(id));
        //修改本身的对象
        Document doc = new Document("$set", new Document("status", status));
        //执行修改
        UpdateResult result = users.updateOne(query, doc);
        long count = result.getModifiedCount();//修改条数
        return count;
    }

    /*public long update1(String id) {
        MongoCollection<Document> users = MongoDBUtil.getCollection("users");
        Bson query = Filters.eq("_id", new ObjectId(id));
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要修改的值 1 用户名 2 密码 3 年龄 4 邮箱 5 电话 6 状态");
        int a = sc.nextInt();
        if (a == 1) {
            String username = sc.next();
            Document doc = new Document("$set", new Document("username", username));
        }else if (a == 2){
            String password = sc.next();
            Document doc = new Document("$set", new Document("password", password));
        }
        //Document doc = new Document("$set", new Document("status", status));
    }*/

    //查询所有
    //无参
    //返回值类型List<User>
    public List<User> findAll() {
        MongoCollection<Document> users = MongoDBUtil.getCollection("users");
        FindIterable<Document> docs = users.find();
        List<User> userList = new ArrayList<User>();
        for (Document doc : docs) {
            //docs中的每个document对象转换为user对象
            User user = new User();
            user.setId( "_id");
            user.setUsername(doc.getString("username"));
            user.setPassword(doc.getString("password"));
            user.setAge(doc.getInteger("age"));
            user.setEmail(doc.getString("email"));
            user.setMobile(doc.getString("mobile"));
            user.setAddress(doc.getString("address"));
            user.setStatus(doc.getInteger("status"));
            userList.add(user);
        }
        return userList;
    }
}
